package com.ui.fragment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import roboguice.inject.InjectView;
import android.os.Bundle;
import android.os.Message;
import android.view.View;

import com.core.sdk.core.BaseEvent;
import com.core.sdk.core.BaseFragment;
import com.core.sdk.core.LogUtil;
import com.core.sdk.ui.waterfall.PullToRefreshWaterfallView;
import com.core.sdk.ui.waterfall.PullToRefreshWaterfallView.OnRefreshListener;
import com.core.sdk.ui.waterfall.internal.PLA_AdapterView;
import com.core.sdk.ui.waterfall.internal.PLA_AdapterView.OnItemClickListener;
import com.core.sdk.utils.IOUtil;
import com.ui.R;
import com.ui.adapter.ShopAdapter;
import com.ui.model.Shop;

public class MainFragment1 extends BaseFragment implements OnRefreshListener, OnItemClickListener {

	String text = null;

	@InjectView(R.id.fg_main1_waterfall)
	PullToRefreshWaterfallView waterfallView;

	ShopAdapter adapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		text = getArguments().getString("data");
	}

	BaseEvent initImagePathEvent = new BaseEvent(getLocation(), getLocation());

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		adapter = new ShopAdapter(getBaesActivity());
		waterfallView.setAdapter(adapter);
		waterfallView.setVisibility(View.VISIBLE);
		waterfallView.setOnRefreshListener(this);
		waterfallView.setOnItemClickListener(this);
		
		waterfallView.setOnScrollStateChangedListenerList(adapter.getListenerList());
		sendEvent(initImagePathEvent);
	}

	private final int WHAT_TOP = 0XAA;
	private final int WHAT_BOTTOM = WHAT_TOP + 1;
	private final int WHAT_LOAD_MORE = WHAT_TOP + 2;

	@Override
	public void onTopRefresh() {
		sendEmptyMessageDelayed(WHAT_TOP, 3000L);
	}

	@Override
	public boolean onBottomRefresh() {
		sendEmptyMessageDelayed(WHAT_BOTTOM, 3000L);
		return true;
	}

	@Override
	public void onItemClick(PLA_AdapterView<?> parent, View view, int position, long id) {
		LogUtil.e(tag, "position=" + position);
	}

	@Override
	protected int getContentView() {
		return R.layout.fg_main1;
	}

	private static final List<String> pathList = new ArrayList<String>();

	@Override
	public void executeEvent(BaseEvent evt) {
		super.executeEvent(evt);
		if (evt == initImagePathEvent) {
			File f = new File(IOUtil.getExternalStoragePath() + "consumer/cache");
			File[] fs = f.listFiles();
			for (File file : fs) {
				String suf = IOUtil.getFileSuffix(file.getAbsolutePath());
				if (!".jpg".equalsIgnoreCase(suf))
					continue;
				pathList.add(file.getAbsolutePath());
			}
			sendEmptyMessage(WHAT_LOAD_MORE);
		}
	}

	@Override
	public void executeMessage(Message msg) {
		super.executeMessage(msg);
		if (msg.what == WHAT_LOAD_MORE) {
			loadMore();
		} else if (msg.what == WHAT_TOP) {
			waterfallView.setTopRefreshComplete();
			loadMore();
		} else if (msg.what == WHAT_BOTTOM) {
			waterfallView.setBottomRefreshComplete();
			loadMore();
		}
	}

	private void loadMore() {
		int c = adapter.getCount();
		for (int i = c; i < c + 50; i++) {
			Shop sh = new Shop(pathList.get(i % pathList.size()), i);
			adapter.addItem(sh, null);
		}
		adapter.notifyDataSetChanged();
	}

}
